<template>
  <div>
    <div class="course_base">
      <el-form>
        <el-form-item label="课程名称">
          <el-input v-model="cname"></el-input>
        </el-form-item>
        <el-form-item label="课程描述">
          <el-input v-model="cdes"></el-input>
        </el-form-item>
        <el-form-item label="课程价格">
          <el-input v-model="cprice"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary"
                     @click="saveCourse">保存</el-button>
        </el-form-item>
      </el-form>

      <div>
        <div class="img_box">
          <div class="img_show">
            <img :src="imgsrc"
                 alt="">
          </div>
          <div>
            <el-button>清除</el-button>
            <el-button @click="dialogVisible=true">上传</el-button>
          </div>
        </div>
      </div>
    </div>
    <!-- 上传图片的弹出框 -->
    <el-dialog title="上传图片"
               :visible.sync="dialogVisible"
               width="30%"
               :close-on-click-modal="false">
      <el-form>
        <el-form-item v-if="upFlag">
          <el-input></el-input>
          <el-button @click="changeFile">选择图片</el-button>
        </el-form-item>
        <div v-if="!upFlag">
          <img :src="imgsrc"
               alt=""
               class="myimg">
          <el-button @click="sureUpload">上传</el-button>
          <el-button>清除</el-button>
        </div>
        <input type="file"
               style="display:none;"
               accept="image/gif,image/jpeg,image/jpg,image/png,image/svg"
               ref="myfile"
               @change="fileChange($event)">
      </el-form>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="dialogVisible=false">取 消</el-button>
        <el-button type="primary">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
import { uploadImg, addCourseApi } from '../../api/course'
export default {
  data () {
    return {
      dialogVisible: false,
      upFlag: true,
      imgsrc: "",//缩略图地址
      cpic: "",//封面地址
      cname: "",
      cprice: "",
      cdes: ""
    }
  },
  methods: {
    changeFile () {
      this.$refs.myfile.click();  //弹出文件框
    },
    fileChange (e) { //选择文件后 触发change事件  开始调用上传接口
      this.upFlag = false;
      // 制作缩略图
      var files = e.target.files || e.dataTransfer.files; //获取选择的文件内容
      if (!files.length || files[0].size > 1024 * 1024 * 1024) return;//如果未选择任何视频或视频大小超过1G就终断操作
      var url = URL.createObjectURL(files[0])

      this.imgsrc = url;
      // 完成上传

    },
    async sureUpload () { //确定上传
      // 上传的数据包内容放到 FormData 传递
      var form = new FormData();
      // form.append(key,value )//value 文件上传框中的数据包
      form.append(Math.random().toString(), this.$refs.myfile.files[0])
      var res = await uploadImg(form)
      console.log(res)
      this.cpic = res.cacheName
      this.dialogVisible = false;
    },
    async saveCourse () { //保存课程  
      var res = await addCourseApi({
        Cname: this.cname,
        Cdescribe: this.cdes,
        Cprice: this.cprice,
        Cpic: this.cpic
      })
      console.log(res)
    }
  },
}
</script>
<style lang="less" scoped>
.course_base {
  width: 1200px;
  margin: 20px auto;
  height: 400px;
  display: flex;
  .el-form {
    width: 800px;
  }
  .img_box {
    width: 400px;
    height: 400px;
    .img_show {
      width: 200px;
      height: 180px;
      border: 1px solid black;
    }
  }
  img {
    width: 200px;
    height: 180px;
  }
}
.myimg {
  width: 200px !important;
  height: 180px !important;
}
</style>